package eu.jba.denshijisho;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import eu.jba.denshijisho.dao.ConfigurationDao;
import eu.jba.denshijisho.entity.Configuration;
import eu.jba.denshijisho.exceptionhandler.ExceptionHandler;
import eu.jba.denshijisho.util.Constants;

public class EditConfigValueActivity extends AbstractDenshiActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Thread.setDefaultUncaughtExceptionHandler( new ExceptionHandler( this ) );
		setFinishOnTouchOutside(false);
		setContentView(R.layout.activity_edit_config_value);
		final Configuration configuration = getConfigurationIdFromExtra();
		((TextView)findViewById(R.id.config_value_name)).setText( configuration.getVariableName() );
		((EditText)findViewById(R.id.integer_config_value_input)).setText(configuration.getIntegerValue().toString());
		((EditText)findViewById(R.id.string_config_value_input)).setText( configuration.getStringValue());
	}
	
	private Configuration getConfigurationIdFromExtra(){
		final Bundle bundle = getIntent().getExtras();
		return (Configuration)bundle.getSerializable(Constants.CONFIGURATION);
	}
	
	public void closeDialog(View view) {
		setResult(RESULT_CANCELED);
		finish();
	}
	
	public void updateConfig(View view) {
		final Configuration configuration = getConfigurationIdFromExtra();
		configuration.setIntegerValue( Integer.parseInt(((EditText)findViewById(R.id.integer_config_value_input)).getText().toString() ));
		configuration.setStringValue(((EditText)findViewById(R.id.string_config_value_input)).getText().toString());
		final ConfigurationDao configurationDao = new ConfigurationDao(this);
		configurationDao.open();
		configurationDao.update( configuration );
		configurationDao.close();
		setResult(RESULT_OK);
		finish();
	}
}
